<template>
  <div class="login-bg">
    <div class="login-title">
      校园二手交易平台后台管理系统
    </div>
    <div class="login-subtitle">
      Campus Second-hand Trading Admin
    </div>
    <el-form
      ref="formRef"
      class="login-form"
      :model="form"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item
        label="用户名"
        prop="username"
      >
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :loading="loading"
          @click="onLogin"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const formRef = ref()
const loading = ref(false)
const form = ref({ username: '', password: '' })
const rules = {
  username: [ { required: true, message: '请输入用户名', trigger: 'blur' } ],
  password: [ { required: true, message: '请输入密码', trigger: 'blur' } ]
}

function onLogin() {
  formRef.value.validate((valid: boolean) => {
    if (!valid) return
    loading.value = true
    setTimeout(() => {
      loading.value = false
      if (form.value.username === 'admin' && form.value.password === '123456') {
        localStorage.setItem('token', 'mock-token')
        router.push('/')
        ElMessage.success('登录成功')
      } else {
        ElMessage.error('用户名或密码错误')
      }
    }, 1000)
  })
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #e0e7ff 0%, #f5f6fa 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.login-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
  letter-spacing: 2px;
}
.login-subtitle {
  font-size: 16px;
  color: #888;
  margin-bottom: 32px;
}
.login-form {
  max-width: 350px;
  padding: 40px 24px;
  box-shadow: 0 2px 12px #eee;
  border-radius: 8px;
  background: #fff;
}
</style> 